#include <assert.h>
#include <unistd.h>
#include "mhdef.h"
#include "dispatch.h"
#include <string.h>
#include <stdio.h>
#include "cmd.h"
#include "udp.h"

#define MH_CAN_DISPATCH     200
#define DISPATCH_TIMEOUT    2000

/*
 * @param msg: 要发给下位机的消息
 * @param rmsg: 接收到的can消息
 * @note: 转发can消息
 *        dualram出错返回-1，can通讯出错返回-2，成功返回０
 */
int dispatch_msg(MH_UDP *pudp,CAN_MSG *msg, CAN_MSG *rmsg)
{
    assert(msg != NULL);

    REQUEST_CAN request;
    int i = 0;
    int ret = 0;

    memset(&request, 0, sizeof (REQUEST_CAN));

    request.cmd = CMD_REQUEST_CAN;
    request.id = msg->id;
    request.len = msg->len;

    memcpy(request.data, msg->data, msg->len);


    for (i = 0; i < 2; i++)
    {
        ret = cmd_request(pudp, (uchar*)&request, sizeof (request));

        if (ret == 0)
        {
            break;
        }
    }
    return ret;
}
